using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Windows.Forms;

namespace GSN
{
    static class Logger
    {
        private static StreamWriter sw = null;

        public static bool isOn() { return (sw != null); }

        public static string GSN_LOG_FILENAME = Path.GetDirectoryName(Application.ExecutablePath) + 
                "\\GSN" /*+ DateTime.Now.Ticks.ToString()*/ + ".log";


        public static void switchOn() 
        { 
            sw = new StreamWriter(GSN_LOG_FILENAME);
        }

        public static void switchOff()
        {
            if (sw != null)
            {
                sw.Close();
                sw = null;
            }
        }

        public static void logException(Exception e)
        {
            log("Message: {0}\r\nStack Trace: {1}",
                e.Message,
                e.StackTrace);

        }

        public static void log(string s, params object[] args)
        {
            if (sw == null)
                return;

            lock (sw)
            {
                string line = string.Format("{0} {1} - {2}",
                    DateTime.Now.ToShortDateString(),
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format(s, args));
                sw.WriteLine(line);
                sw.Flush();
            }
        }

    }
}
